package com.assessment.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.PerformanceInterviewRepository;
import com.assessment.dto.PerformanceInterviewDTO;
import com.assessment.entity.PerformanceInterview;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;


@Service
@Transactional
public class PerformanceInterviewService implements IPerformanceInterviewService{

	@Autowired
	private PerformanceInterviewRepository performanceInterviewRepository;
	@Autowired
	private StaffRepository staffRepository;
	@Override
	public void saveOrUpdate(PerformanceInterviewDTO performanceInterviewDTO) {
		// TODO Auto-generated method stub
		PerformanceInterview performanceInterview =new PerformanceInterview();
		BeanUtils.copyProperties(performanceInterviewDTO,performanceInterview);
		Staff staff=staffRepository.findOne(performanceInterviewDTO.getStaffId());		
		Staff superior=staffRepository.findOne(performanceInterviewDTO.getSuperiorId());
		performanceInterview.setStaff(staff);
		performanceInterview.setSuperior(superior);
		performanceInterviewRepository.save(performanceInterview);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		PerformanceInterview performanceInterview=performanceInterviewRepository.findOne(id);
		if(performanceInterview!=null) {
			performanceInterview.setStaff(null);
			performanceInterview.setSuperior(null);
			performanceInterviewRepository.save(performanceInterview);
			performanceInterviewRepository.delete(performanceInterview);
		}		
	}

	@Override
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<PerformanceInterview> performanceInterviewList=(List<PerformanceInterview>) performanceInterviewRepository.findAll(ids);
		if(performanceInterviewList!=null) {
			for(PerformanceInterview performanceInterview :performanceInterviewList) {
				performanceInterview.setStaff(null);
				performanceInterview.setSuperior(null);
				performanceInterviewRepository.save(performanceInterview);
			}
			performanceInterviewRepository.delete(performanceInterviewList);
		}		
	}

	@Transactional(readOnly=true)
	public PerformanceInterview findOne(Long id) {
		// TODO Auto-generated method stub
		return performanceInterviewRepository.findOne(id);
	}

	@Transactional(readOnly=true)
	public List<PerformanceInterview> findAll() {
		// TODO Auto-generated method stub
		return (List<PerformanceInterview>) performanceInterviewRepository.findAll();
	}

	@Transactional(readOnly=true)
	public Page<PerformanceInterviewDTO> findAll(Specification<PerformanceInterview> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		List<PerformanceInterviewDTO> results = new ArrayList<PerformanceInterviewDTO>();
		Page<PerformanceInterview> page=performanceInterviewRepository.findAll(spec, pageable);
		for(PerformanceInterview performanceInterview:page.getContent()) {
			PerformanceInterviewDTO performanceInterviewDTO = new PerformanceInterviewDTO();
        	BeanUtils.copyProperties(performanceInterview, performanceInterviewDTO);

        	performanceInterviewDTO.setStaffId(performanceInterview.getStaff().getId());
        	performanceInterviewDTO.setStaffName(performanceInterview.getStaff().getStaffName());
        	performanceInterviewDTO.setSuperiorId(performanceInterview.getSuperior().getId());
        	performanceInterviewDTO.setSuperiorName(performanceInterview.getSuperior().getStaffName());
        	results.add(performanceInterviewDTO);
		}
		return new PageImpl<PerformanceInterviewDTO>(results, pageable, page.getTotalElements());
	}

}
